package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;

public class _NxNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://wz.nxnews.cn/.*/.*/[0-9]{6}/.*.html",
		"http://i.nxnews.net/[a-z]*-[0-9]*-1.html"
	};
	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}



	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String url=urlMeta.getUrl();
		String htmltxt=urlMeta.getHtml();

		if(!isDetailPage(url)){
		}


		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title= doc.select("title").text();
		title=StringUtils.substringBefore(title, "-");
		content=HtmlCleaner.getContentHtml(url,doc.select("div.Custom_UnionStyle"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.d"));
		}
		date=DateUtils.matchDate(doc.select("p.xg1").text());
		news.setUrl(url);
		news.setTitle(title);
		news.setContent(content);
		news.setDate(date);
		String author = JSoupUtils.matchAuthor(doc, "来源：");
		if(author==null){
			author = "宁夏网";
		}
		news.setAuthor(author);
		return news;
	}

	public static void main(String[] args) {
		_NxNewsAnalyse test=new _NxNewsAnalyse();
		String url="http://i.nxnews.net/article-26017-1.html";
		url = UrlArgumentTop.FromatUrl(url);
//		System.out.println(UrlArgumentTop.FromatUrl(url));
		System.out.println(test.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=test.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
